package com.mansoor.app.islamicnames.dao;

import com.mansoor.app.islamicnames.Predicates.CountPage;
import com.mansoor.app.islamicnames.Predicates.SearchNameList;
import com.mansoor.app.islamicnames.beans.Name;
import com.mansoor.app.islamicnames.util.AppConstants;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.math.util.MathUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Mansoor
 * Date: Oct 14, 2009
 * Time: 11:08:19 PM
 * To change this template use File | Settings | File Templates.
 */
public class NamesDaoImpl implements NamesDao
{
    private InMemoryDB db;
    private Map<String,Integer> countMap= new HashMap<String,Integer>();

    public NamesDaoImpl(InMemoryDB db)
    {
        this.db = db;
    }


    /**
     * Method getNames ...
     *
     * @param alphabets of type String
     * @return List<Name>
     */
    public List<Name> getNames(String alphabets, Integer page)
    {
        final List<Name> names = db.getNames();
        final List<Name> result = new ArrayList<Name>();
        final SearchNameList searchName = new SearchNameList(alphabets, null, page);

        CollectionUtils.select(names, searchName, result);
        return result;

    }

    /**
     * Method getNames ...
     *
     * @param alphabets of type String
     * @param gender    of type String
     * @return List<Name>
     */
    public List<Name> getNames(String alphabets, String gender, Integer page)
    {
        final List<Name> names = db.getNames();
        final List<Name> result = new ArrayList<Name>();
        final SearchNameList searchName = new SearchNameList(alphabets, gender, page);

        CollectionUtils.select(names, searchName, result);
        return result;
    }

    /**
     * Method getName ...
     *
     * @param name of type String
     * @return Name
     */
    public Name getName(String name)
    {
        String key= StringUtils.capitalize(name);
        final Map<String, Name> map = db.getNameMap();


        return map.get(key);
    }

    /* public List<Name> getAll()
    {
        final Name query = new Name();

        final Iterator objectSet = template.get(query).iterator();

        return IteratorUtils.toList(objectSet);
    }*/


    public Integer count(String alphabets)
    {


        final List<Name> names = db.getNames();
        final List<Name> result = new ArrayList<Name>();
        final CountPage countPage = new CountPage(alphabets, null);

        CollectionUtils.select(names, countPage, result);
        final int size = result.size();
        Double pageCont = ((double) size) / AppConstants.RECORD_PER_PAGE;
        final Double v = MathUtils.round(pageCont, 0, BigDecimal.ROUND_UP);
        return v.intValue();
        //To change body of implemented methods use File | Settings | File Templates.

    }

    public Integer count(String alphabets, String gender)
    {
          Integer resInteger=null;

        final Double v;
        final String key = alphabets + gender;

        if (countMap.get(key)==null)
        {
            final List<Name> names = db.getNames();
            final List<Name> result = new ArrayList<Name>();
            final CountPage countPage = new CountPage(alphabets, gender);

            CollectionUtils.select(names, countPage, result);

            final int size = result.size();
            Double pageCont = ((double) size) / AppConstants.RECORD_PER_PAGE;
            v = MathUtils.round(pageCont, 0, BigDecimal.ROUND_UP);
            resInteger=v.intValue();
            countMap.put(key,resInteger);
        }

        else
        {
            resInteger=countMap.get(key);
        }
        return resInteger;
    }
}
